import 'package:flutter/material.dart';

import 'dart:convert' as convert;
import 'package:http/http.dart' as http;

void main() {
  runApp(MyApp());
}

//自定义组件
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // MaterialApp是根组件
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('Dialog Web'),
          ),
          body: WebRequestDemo()),
      theme: ThemeData(primaryColor: Colors.blue),
    );
  }
}

class WebRequestDemo extends StatefulWidget {
  WebRequestDemo({Key? key}) : super(key: key);

  @override
  _WebRequestDemoState createState() => _WebRequestDemoState();
}

class _WebRequestDemoState extends State<WebRequestDemo> {
  List result = [];

  @override
  void initState() {
    super.initState();
    _getDate();
  }

  _getDate() async {
    var url = Uri.parse('http://localhost:8080/address');
    print(url);
    var response = await http.get(url);
    if (response.statusCode == 200) {
      setState(() {
        result = convert.jsonDecode(response.body) as List<dynamic>;
        print(' $result.');
      });
    } else {
      print(
          'Request failed with status: ${response.statusCode}. ${response.statusCode}');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: this.result.length > 0
          ? Text(result[0]['date'] + result[0]['name'] + result[0]['address'])
          : Text("no get date"),
    );
  }
}
